package com.xinfu.newproject.ui.matter_repair;


import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.adapter.matter_repair_adapter.MatterListAdapter;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.bean.matter_repair.RepairDetailBean;
import com.xinfu.newproject.bean.matter_repair.RepairRecordBean;
import com.xinfu.newproject.bean.matter_repair.bean.RepairCountBean;
import com.xinfu.newproject.presenter.RepairRecordPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;


/**
 * 报修
 */
public class RepairPartsFragment extends BaseActivity implements IView.IRepairRecordView {

    @BindView(R.id.fragment_repair_parts_rv)
    RecyclerView mRecyclerView;

    @BindView(R.id.fragment_repair_parts_srl)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private MatterListAdapter matterListAdapter;

    private RepairRecordPresenter repairRecordPresenter = new RepairRecordPresenter(this);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_repair_parts);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("报修记录");
        initViewBind();
        initLisener();
    }

    private void initLisener() {

    }

    private void initViewBind() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        matterListAdapter = new MatterListAdapter();
        mRecyclerView.setAdapter(matterListAdapter);
        initRefreshLayout();
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        int ok = isLogin(0);
        if (ok == 1) {
            repairRecordPresenter.repairRecord();
        }
    }


    /**
     * 下拉刷新
     */
    private void initRefreshLayout() {
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                int ok = isLogin(0);
                if (ok == 1) {
                    repairRecordPresenter.repairRecord();
                }
            }
        });
    }


    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private Integer isLogin(int type) {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            return 0;
        }


        return 1;
    }

    @Override
    public String header() {
        return token;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String repairId() {
        return "";
    }

    @Override
    public void showToast(String content) {
        BToast.showText(this, content);
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void getData(RepairRecordBean repairRecordBean) {
        matterListAdapter.setNewData(repairRecordBean.getResult());
        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void getData(RepairDetailBean repairDetailBean) {

    }

    @Override
    public void getData(RepairCountBean repairCountBean) {

    }
}
